GBML manual / Scripts

Scripts

A script is a named, callable piece of GBML kept in its own file - how code gets shared between events, objects, and cutscenes.

What a script is

Every object's code lives in its events - create.gbml, step.gbml, and so on - and an event only ever runs when the engine decides to run it. A script is the other kind of file: one .gbml file under scripts/, holding one named function that does nothing on its own and runs only when something calls it - an event, a cutscene step, or another script. Functions already covers the language side of this ("Scripts versus events"); this section is the deeper dive - how to write one, what its parameters can and cannot do, and what actually ends up in the ROM.

A script compiles to an ordinary C++ function, sitting right next to the engine's own hand-written code - see What a script compiles to for exactly what that looks like and what it costs.

Why reach for one

The same handful of lines, copied into two or more events, is the one sign it is time to move them into a script instead: write the logic once, call it from wherever it is needed, and a later change only has to be made in one place. A weapon's damage formula, a pickup's "give the player this and play a sound" routine, or a boss fight's own attack pattern are all naturally scripts for exactly this reason - several objects, or several events on the same object, all need the same behavior.

The rest of this section

A script file does not have to hold a callable function at all

An empty script file is legitimate, and so is one that holds nothing but a shared enum, #macro or struct declaration meant for other files to use, with no function in it at all - neither is an error by itself. GBML only minds the moment something actually tries to call the script and finds nothing there to run - see Writing a script for exactly what that error says and when it fires. A project can use a script file purely as a place to put a declaration several objects need to share.

Where scripts live in the editor

The Code tab's project browser lists scripts alongside objects, and creates, renames, duplicates and deletes them the same way - see The Code tab. A script can also be filed into a named group there, the same organizing idea as a folder, purely for keeping a large project browsable; it changes nothing about how the script compiles or how it is called.

See also: FunctionsWhat GBML isWriting a scriptScript parametersWhat a script compiles toThe Code tablist_foreachlist_filterarray_foreachgrid_foreach